package net.tv90.bfmccore.listener;

import org.bitterorange.flansmodslain.FlansModAPI;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.LeavesDecayEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NotBuildListener implements Listener {

    public static final String[] whiteListWorld = {};

    public static final String[] blackListWorld = {"world", "Tutorial", "TrainingArea"};

    @EventHandler(priority = EventPriority.HIGHEST)
    public void HangingBreakByEntityEvent(final HangingBreakByEntityEvent event) {
        if (event.getRemover() instanceof Player) {
            Player player = (Player) event.getRemover();
            World world = player.getWorld();
            Block block = event.getEntity().getLocation().getBlock();
            for (String s : blackListWorld) {
                if(world.getName().equals(s)) {
                    if(!player.isOp()) {
                        event.setCancelled(true);
                    }
                }
            }
            if(!player.isOp()) {
                event.setCancelled(true);
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void BreakBlock(final BlockBreakEvent event) {
        Player player = event.getPlayer();
        World world = player.getWorld();
        Block block = event.getBlock();
        for (String s : blackListWorld) {
            if(world.getName().equals(s)) {
                if(!player.isOp()) {
                    event.setCancelled(true);
                }
            }
        }
        if(!FlansModAPI.canBeBrokenByPlayer(block)) {
            if(!player.isOp()) {
                event.setCancelled(true);
            }
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void PlaceBlock(final BlockPlaceEvent event) {
        Player player = event.getPlayer();
        World world = player.getWorld();
        Block block = event.getBlock();
        for (String s : blackListWorld) {
            if(world.getName().equals(s)) {
                if(!player.isOp()) {
                    event.setCancelled(true);
                }
            }
        }
        if(!FlansModAPI.canBeBrokenByPlayer(block)) {
            if(!player.isOp()) {
                event.setCancelled(true);
            }
        }
    }


    @EventHandler(priority = EventPriority.LOWEST)
    public void HangingPlace(final HangingPlaceEvent event) {
        Player player = event.getPlayer();
        World world = player.getWorld();
        Block block = event.getBlock();
        for (String s : blackListWorld) {
            if(world.getName().equals(s)) {
                if(!player.isOp()) {
                    event.setCancelled(true);
                }
            }
        }
        if(!player.isOp()) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void Fill(final PlayerBucketFillEvent event) {
        Player player = event.getPlayer();
        World world = player.getWorld();
        for (String s : blackListWorld) {
            if(world.getName().equals(s)) {
                if(!player.isOp()) {
                    event.setCancelled(true);
                }
            }
        }
        if(!player.isOp()) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void Empty(final PlayerBucketEmptyEvent event) {
        Player player = event.getPlayer();
        World world = player.getWorld();
        for (String s : blackListWorld) {
            if(world.getName().equals(s)) {
                if(!player.isOp()) {
                    event.setCancelled(true);
                }
            }
        }
        if(!player.isOp()) {
            event.setCancelled(true);
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void LeavesDecay(final LeavesDecayEvent event) {

    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void BlockFade(final BlockFadeEvent event) {

    }

}
